<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item class="merge-item">
        <el-select v-model="qtype" style="width: 190px; margin-right: 2px;">
          <el-option value="chipCode" label="企业气瓶码" />
          <el-option value="gpSerial" label="省统一码" />
        </el-select>
        <el-input v-model.trim="txt" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="所属机构:">
        <el-tree-select
          v-model="query.bindOrgSNNo"
          clearable
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData,
          }"
          :tree-params="{
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props: {
              value: 'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData,
          }"
          @node-click="selectTreeChange"
          @select-clear="closeTree"
        />
      </el-form-item>
      <el-form-item label="绑码人:">
        <el-select
          v-model="query.bindOwnerId"
          :loading="$loadingPlugin.getOwnerList"
          filterable
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in ownerList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="绑码时间:" :table-form-merge="2" class="merge-item">
        <el-date-picker
          v-model="date"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          unlink-panels
          :clearable="false"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <template v-slot:btns>
        <el-button
          v-permission="[70901,1]"
          :loading="$loadingPlugin.fetchData"
          type="primary"
          icon="el-icon-search"
          @click="search"
        >查询</el-button>
      </template>
    </el-table-form>
    <div class="table_Main">
      <lb-table
        ref="lbTable"
        v-loading="$loadingPlugin.fetchData"
        v-el-height-adaptive-table="{ bottomOffset: 10 }"
        height="250"
        border
        highlight-current-row
        element-loading-text="加载中"
        :column="column"
        :data="tableData.data"
        pagination
        :total="tableData.total"
        custom-column
        default-open
        @fetch="fetchData"
      />
    </div>
  </div>
</template>

<script>
  import { queryOwnerList, bindSelectList } from '@/api'
  import { parseTime } from '@/utils'
  import getTreeData from '@/utils/jsonTree'
  import moment from 'dayjs'
  import { mapGetters } from 'vuex'
  export default {
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            return moment(time.getTime()).diff(moment(Date.now()), 'days') < 0
          }
        },
        treeData: [],
        currentOrg: null,
        ownerList: [],
        qtype: 'chipCode',
        txt: '',
        // date: [moment().startOf('day'), moment().endOf('day')],
        date: [],
        query: {
          bindOrgSNNo: '', // 所属机构orgSNNo
          bindOwnerId: '' // 解绑人id
        },
        tableData: {
          total: 0,
          data: []
        }
      }
    },
    computed: {
      ...mapGetters(['orgList', 'corpSettingData']),
      column: function() {
        return [
          {
            type: 'index',
            fixed: 'left'
          },
          {
            prop: 'chipCode',
            label: '企业气瓶码'
          },
          {
            prop: 'gpSerial',
            label: '省统一码'
          },
          {
            prop: 'bindOrgName',
            label: '所属机构'
          },
          {
            prop: 'bindOwnerName',
            label: '绑码人'
          },
          {
            prop: 'bindTime',
            label: '绑码时间',
            render: (h, { row }) => (
            <span>
              {row.bindTime && parseTime(row.bindTime, '{y}-{m}-{d} {h}:{i}:{s}')}
            </span>
            )
          }
        ]
      }
    },
    async created() {
      await Promise.all([
        this.getTreeData(),
        this.getOwnerList()
      ])
    },
    methods: {
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map((item) => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        query[this.qtype] = this.txt
        if (this.date && this.date.length > 0) {
          query.bindTime = this.date[0]
          query.bindTimeEnd = this.date[1]
        }
        const res = await bindSelectList(query)
        if (res.data) {
          this.tableData.data = res.data.rows
          if (currentPage === 1) this.tableData.total = res.data.total
        }
      },
      // 选择所属机构
      selectTreeChange(value) {
        this.currentOrg = value
        this.getOwnerList()
      },
      // 清空所属机构
      closeTree(v) {
        this.ownerList = []
        this.query.bindOwnerId = ''
        this.getOwnerList()
      },
      // 获取处理人列表
      async getOwnerList() {
        const b = (this.corpSettingData.sp1 >> 2) & 0x01
        const orgId = this.currentOrg ? this.currentOrg.orgId : -1
        const res = await queryOwnerList({
          orgId,
          corpparm1: b
        })
        this.ownerList = res.data
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>

<style lang="scss" scoped>
.table_Main {
  position: relative;
  .total {
    position: absolute;
    right: 5px;
    bottom: 5px;
    font-size: 16px;
    font-weight: 500;
    // color: #F00;
  }
}
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
/deep/ .el-table tr.danger-row {
  background: #ffeced;
}
/deep/ .el-table tr.giveup-row {
  background: #ffeced;
}
/deep/.red {
  color: #f00 !important;
}
/deep/.yellow {
  color: #ed912e !important;
}

</style>
